<template>
  <div id="addPictureBatchPage">
    <h2 style="margin-bottom: 16px">批量创建图片</h2>
    <a-form layout="vertical" :model="formData" @finish="handleSubmit" >
      <a-form-item label="关键词" name="searchText">
        <a-input v-model:value="formData.searchText" placeholder="请输入关键词" />
      </a-form-item>
      <a-form-item label="抓取数量" name="count">
        <a-input-number
          v-model:value="formData.count"
          placeholder="请输入数量"
          style="min-width: 180px"
          :min="1"
          :max="30"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="名称前缀" name="namePrefix">
        <a-input v-model:value="formData.namePrefix" placeholder="请输入名称前缀，会自动补充序号" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%" :loading="loading"> 执行任务</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { useRoute } from 'vue-router'
import { getPictureVoByIdUsingGet, uploadPictureByBatchUsingPost } from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'
import router from '@/router'

//参数定义
const picture = ref<API.PictureVO>()
const formData = reactive<API.PictureUploadByBatchRequest>({
  count: 10,
})
const categoryOptions = ref<String[]>([])
const tagOptions = ref<String[]>([])
//获取上传图片信息
const onSuccess = (newPicture: API.PictureVO) => {
  picture.value = newPicture
  formData.name = newPicture.name
}

const loading = ref(false)
/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: any) => {
  message.info('Crawling in progress, please wait patiently.')
  loading.value = true
  const res = await uploadPictureByBatchUsingPost({
    ...formData,
  })
  if (res.data.code === 0 && res.data.data) {
    message.success(`创建成功，共 ${res.data.data} 条`)
    router.push({
      path: '/',
    })
  } else {
    message.error('创建失败，' + res.data.message)
  }
  loading.value = false
}

// 获取老数据
const route = useRoute()
const getOldPicture = async () => {
  try {
    // 获取数据
    const id = route.query?.id
    if (id) {
      const res = await getPictureVoByIdUsingGet({
        id: id,
      })
      if (res.data.code === 0 && res.data.data) {
        const data = res.data.data
        picture.value = data
        formData.name = data.name
        formData.introduction = data.introduction
        formData.category = data.category
        formData.tags = data.tags
      }
    }
  } catch (e) {
    message.error('请联系管理员！')
  }
}
onMounted(() => {
  getOldPicture()
})
</script>
<style scoped>
#addPictureBatchPage {
  max-width: 720px;
  margin: 0 auto;
}
</style>
